# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from lxml import etree
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# 初始化
def init_driver(url):
    chrome_options = Options()
    # chrome_options.add_argument('--headless') # 无头模式
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) # 设置开发模式，避免被检测
    # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
    # 未在PATH环境变量中设置，需要设置驱动可执行路径
    driver = webdriver.Chrome(executable_path='D:\\webdriver\\chromedriver.exe', chrome_options=chrome_options)
    driver.maximize_window()  # 窗口最大化
    driver.get(url)
    # html = driver.page_source
    # print(html)
    return driver


# 获取页面url
def get_url(drive):
    # 设置等待
    wait = WebDriverWait(driver, 10)

    html = etree.HTML(driver.page_source)
    xpath = html.xpath('//div[@class="txt-box"]')
    for x in html.xpath('//div[@class="txt-box"]'):
        title = ""
        titleUrl = ""
        titleContent = ""
        titleTime = ""
        titleFqr = ""
        for t in x.xpath('./h3/a//text()'):
            title += t
        titleUrl = x.xpath('./h3/a/@href')[0]
        for c in x.xpath('./p[@class="txt-info"]//text()'):
            titleContent += c
        titleFqr = x.xpath('./div[@class="s-p"]/a/text()')[0]
        titleTime = x.xpath('./div[@class="s-p"]/span/text()')[0]
    page = html.xpath('.//div[@class="p-fy"]/span')[0].text
    print(page)
    time.sleep(3)
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "sogou_next"))).click()
        # driver.find_element_by_id('sogou_next').click()
    except:
        close_driver(driver)



def close_driver(driver):
    driver.quit()

if __name__ == '__main__':
    url = "https://item.jd.com/1670646.html"
    driver = init_driver(url)
    for page in range(11):
        get_url(driver)
